package com.lonty.intelligence.service;

import com.lonty.intelligence.config.FileConfig;
import com.lonty.intelligence.mapper.CommunityNotificationMapper;
import com.lonty.intelligence.mapper.CommunityNotificationUserMapper;
import com.lonty.intelligence.mapper.UserInfoMapper;
import com.lonty.intelligence.model.CommunityNotification;
import com.lonty.intelligence.model.CommunityNotificationUser;
import com.lonty.intelligence.request.CommunityNotificationList;
import com.lonty.intelligence.util.UUIDUtil;
import com.lonty.intelligence.vo.CommunityNotificationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author 陈璇
 * @Date 2020/5/28 14:50
 * @Version 1.0
 */
@Service
public class CommunityNotificationService {

    @Resource
    private CommunityNotificationMapper communityNotificationMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private CommunityNotificationUserMapper communityNotificationUserMapper;

    @Resource
    private FileConfig fileConfig;

    /**
     * 保存社区通知
     * @param communityNotificationVo
     * @return
     */
    @Transactional
    public boolean saveCommunityNotification(CommunityNotificationVo communityNotificationVo){
        //保存社区通知
        CommunityNotification communityNotification = new CommunityNotification();
        BeanUtils.copyProperties(communityNotificationVo, communityNotification);
        communityNotification.setId(UUIDUtil.randomID());
        communityNotification.setCreateTime(LocalDateTime.now());
        communityNotification.setIsDel(0);
        communityNotificationMapper.saveCommunityNotification(communityNotification);
        //获取所有用户ID
        String id = userInfoMapper.getAllUserId();
        String[] ids = id.split(",");
        //保存社区通知接收人
        if(communityNotificationUserMapper.saveBatchCommunityNotificationUser(ids, communityNotification.getId()) > 0){
            return true;
        }
        return false;
    }

    /**
     * 统计用户未读
     * @param userInfoId
     * @return
     */
    public int countUnread(String userInfoId){
        return communityNotificationUserMapper.countUnread(userInfoId);
    }

    /**
     * 获取用户所有社区通知
     * @param userInfoId
     * @return
     */
    public List<CommunityNotificationList> getCommunityNotificationList(String userInfoId){
        return communityNotificationMapper.getCommunityNotificationList(userInfoId);
    }

    /**
     * 获取社区通知详情
     * @param userInfoId
     * @param communityNotificationId
     * @return
     */
    @Transactional
    public CommunityNotificationList getCommunityNotification(String userInfoId, String communityNotificationId){
        //修改社区通知状态
        communityNotificationUserMapper.modifyStatus(userInfoId, communityNotificationId);
        return communityNotificationMapper.getCommunityNotification(userInfoId, communityNotificationId);
    }

    /**
     * 删除通知
     * @param id
     * @return
     */
    public boolean deleteNotification(String id){
        return communityNotificationMapper.deleteNotification(id) > 0;
    }

}
